package com.cjm.algorithm.node;

import com.cjm.algorithm.ListNode;

public class Ti240506142 {
    public static void main(String[] args) {
        Ti240506142 t = new Ti240506142();
    }

    public ListNode detectCycle(ListNode head) {
        ListNode slow = head;
        ListNode fast = head;
        while (true) {
            if (fast == null || fast.next == null) {return null;}
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                break;
            }
        }
        fast = head;
        while (fast != slow) {
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }
}
